<?php
/**
 * 会员登录管理系统. 
 */
defined("APP_VERSION") OR die("No Access");
class MemberAction extends CoreAction {
	/**
	 * 登录用户信息. 
	 */
	public $member = null;
	
	public function initAction() {
		session_start();
		$this->member = $_SESSION['member'];
		
		if (!isset($this->member['member_id'])) {
			$this->redirect('login', 'index');
		}
	}
	
	/**
	 * 主页面,个人资料修改. 
	 */
	public function index() {
		$this->display();
	}
	
	//右侧页面
	public function main() {
		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post' && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
			$data = array();
			$data['local_city'] = isset($_POST['userCityName']) ? $_POST['userCityName'] : '';
			$data['nickname'] = isset($_POST['userNickName']) ? $_POST['userNickName'] : '';
			$data['sex'] = isset($_POST['userSex']) ? intval($_POST['userSex']) : '';
			$data['brief'] = isset($_POST['userSign']) ? $_POST['userSign'] : '';
			
			//检测用户昵称
			if (!preg_match('/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]{2,10}$/u', $data['nickname'])) {
				echo json_encode(array('code'=> 1, 'message' => '请输入正确的用户昵称.'));
			} else if (!preg_match('/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]{2,10}$/u', $data['local_city'])) {
				echo json_encode(array('code'=> 1, 'message' => '请输入正确的常驻地名.'));
			} else {
				$member_model = new MemberModel();
				if ($member_model->updateMember($data, $this->member['member_id'])) {
					echo json_encode(array('code'=> 0, 'message' => '更新成功.'));
					$_SESSION['member']['nickname'] = $data['nickname'];
				} else {
					echo json_encode(array('code'=> 1, 'message' => '更新失败.'));
				}
			}
		} else {
			$member_model = new MemberModel();
			$this->data['basic_info'] = $member_model->getBasicInfo($this->member['member_id']);
			$this->display();
		}
	}
	
	//详细信息
	public function more() {
		$member_obj = new MemberModel();
		//AJAX 表单数据提交.
		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post' && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
			$data = array();
			$data['tixing'] = isset($_POST['bodily']) ? $_POST['bodily'] : '';
			$data['love_status'] = isset($_POST['loveStatus']) ? $_POST['loveStatus'] : '';
			$data['qq'] = isset($_POST['userQQ']) ? $_POST['userQQ'] : '';
			$data['msn'] = isset($_POST['userMSN']) ? $_POST['userMSN'] : '';
			$data['hangye'] = isset($_POST['job']) ? $_POST['job'] : '';
			$data['daxue'] = isset($_POST['university']) ? $_POST['university'] : '';
			$data['gaozhong'] = isset($_POST['middleSchool']) ? $_POST['middleSchool'] : '';
			$data['home_page'] = isset($_POST['userHomePage']) ? $_POST['userHomePage'] : '';
			$data['aihao'] = isset($_POST['interest']) ? $_POST['interest'] : '';
			$data['movie'] = isset($_POST['movie']) ? $_POST['movie'] : '';
			$data['music'] = isset($_POST['music']) ? $_POST['music'] : '';
			$data['book'] = isset($_POST['book']) ? $_POST['book'] : '';
			$data['dish'] = isset($_POST['dish']) ? $_POST['dish'] : '';
			
			if ($member_obj->updateMemberInfo($data, $this->member['member_id'])) {
				echo json_encode(array('code'=>0,'message'=>'用户详细信息更新成功.'));
			} else {
				echo json_encode(array('code'=>1,'message'=>'用户详细信息更新失败.'));
			}
			exit;
		}
		
		$this->data['member'] = $member_obj->getMemberInfo($this->member['member_id']);
		$this->display();
	}
	
	//个人资料
	public function userface() {
		$this->display();
	}
	
	//LF信息
	public function lfinfo() {
		$this->display();
	}
	
	//LF信息添加编辑.
	public function lfadd() {
		$this->display();
	}
	
	/**
	 * 用户退出登录.
	 */
	public function logout() {
		unset($_SESSION['member']);
		$this->redirect('index', 'index');
	}
}